package com.hj.controller;

import com.hj.dto.CaptchaDto;
import com.hj.constants.CaptchaConstants.RepCodeEnum;
import com.hj.factory.CaptchaTypeFactory;
import com.hj.service.CaptchaService;
import com.hj.vo.CaptchaVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author doudh
 * @date 2020/8/25
 * @description 图形验证码controller
 */
@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    /**
     * 获取图片
     *
     * @param captchaVO
     * @return
     */
    @PostMapping("/get")
    public CaptchaDto get(@RequestBody CaptchaVO captchaVO) {
        CaptchaService captchaService = CaptchaTypeFactory.getCaptchaService(captchaVO.getCaptchaType());
        if (captchaService == null) {
            return RepCodeEnum.PARAM_TYPE_ERROR.parseError("captchaType");
        }
        return captchaService.get(captchaVO);
    }

    /**
     * 校验
     *
     * @param captchaVO
     * @return
     */
    @PostMapping("/check")
    public CaptchaDto check(@RequestBody CaptchaVO captchaVO) {
        CaptchaService captchaService = CaptchaTypeFactory.getCaptchaService(captchaVO.getCaptchaType());
        if (captchaService == null) {
            return RepCodeEnum.PARAM_TYPE_ERROR.parseError("captchaType");
        }
        return captchaService.check(captchaVO);
    }

    /**
     * 二次校验
     *
     * @param captchaVO
     * @return
     */
    @PostMapping("/verify")
    public CaptchaDto verify(@RequestBody CaptchaVO captchaVO) {
        CaptchaService captchaService = CaptchaTypeFactory.getCaptchaService(captchaVO.getCaptchaType());
        if (captchaService == null) {
            return RepCodeEnum.PARAM_TYPE_ERROR.parseError("captchaType");
        }
        return captchaService.verification(captchaVO);
    }
}
